precision mediump float;

uniform sampler2D u_sampler2D;

uniform vec3 u_lightPosition;
uniform vec4 u_lightAmbient;
uniform vec4 u_lightDiffuse;
uniform float u_lightConstantAttenuation;
uniform float u_lightLinearAttenuation;
uniform float u_lightQuadraticAttenuation;

varying vec3 v_positionW;
varying vec3 v_normalW;
varying vec2 v_uv;

void main()
{
	vec4 tex = texture2D(u_sampler2D, v_uv);

	float distance = length(u_lightPosition - v_positionW);
	vec3 light = normalize(u_lightPosition - v_positionW);
	float NDotL = max(0.0, dot(v_normalW, light));
	float attenuation = 1.0 / (u_lightConstantAttenuation + u_lightLinearAttenuation * distance + u_lightQuadraticAttenuation * distance * distance);

	vec4 ambient = u_lightAmbient;
	vec4 diffuse = u_lightDiffuse * NDotL * attenuation;

	gl_FragColor = vec4((tex * (ambient + diffuse)).xyz, 1.0);
}
